<template>
  <div class='sub-sort'>
    <div class="sort">
      <a href="javascript:;" @click="changeSort(null)" :class="{active:sortParams.sortField===null}">默认排序</a>
      <a @click="changeSort('publishTime')" :class="{active:sortParams.sortField==='publishTime'}" href="javascript:;">最新商品</a>
      <a @click="changeSort('orderNum')" :class="{active:sortParams.sortField==='orderNum'}" href="javascript:;">最高人气</a>
      <a @click="changeSort('evaluateNum')" :class="{active:sortParams.sortField==='evaluateNum'}" href="javascript:;">评论最多</a>
      <a  href="javascript:;" @click="changeSort('price')">
        价格排序
        <i class="arrow up"  :class="{active:sortParams.sortField==='price' && sortParams.sortMethod==='asc'}"/>
        <i class="arrow down"  :class="{active:sortParams.sortField==='price' && sortParams.sortMethod==='desc'}"/>
      </a>
    </div>
    <div class="check">
      <XtxCheckbox @change="changeCheck" v-model="sortParams.inventory">仅显示有货商品</XtxCheckbox>
      <XtxCheckbox @change="changeCheck" v-model="sortParams.onlyDiscount">仅显示特惠商品</XtxCheckbox>
    </div>
  </div>
</template>
<script setup>
// 实现按钮交互
// 定义交互数据绑定
const sortParams = reactive({
  inventory: false, // 库存check
  onlyDiscount: false, // 特惠check
  sortField: null, // 默认排序[publishTime,orderNum,price,evaluateNum]
  sortMethod: null // 排序规则，asc为正序，desc为倒序，默认为desc
})
const emit = defineEmits(['sort-change'])
// 点击按钮修改数据
const changeSort = (sortField) => {
  if (sortField === 'price') {
    if (sortParams.sortMethod === null) { //
      sortParams.sortMethod = 'desc'
    } else {
      sortParams.sortMethod = sortParams.sortMethod === 'desc' ? 'asc' : 'desc'
    }
  } else { // 如果已选中，就不用操作了
    if (sortParams.sortField === sortField) {
      return
    }
    sortParams.sortMethod = null
  }
  sortParams.sortField = sortField
  // 触发自定义事件
  emit('sort-change', sortParams)
}
const changeCheck = () => {
  emit('sort-change', sortParams)
}
</script>
<style scoped lang='less'>
.sub-sort {
  height: 80px;
  display: flex;
  align-items: center;
  justify-content: space-between;

  .sort {
    display: flex;

    a {
      height: 30px;
      line-height: 28px;
      border: 1px solid #e4e4e4;
      padding: 0 20px;
      margin-right: 20px;
      color: #999;
      border-radius: 2px;
      position: relative;
      transition: all .3s;

      &.active {
        background: @xtxColor;
        border-color: @xtxColor;
        color: #fff;
      }

      .arrow {
        position: absolute;
        border: 5px solid transparent;
        right: 8px;

        &.up {
          top: 3px;
          border-bottom-color: #bbb;

          &.active {
            border-bottom-color: @xtxColor;
          }
        }

        &.down {
          top: 15px;
          border-top-color: #bbb;

          &.active {
            border-top-color: @xtxColor;
          }
        }
      }
    }
  }

  .check {
    .xtx-checkbox {
      margin-left: 20px;
      color: #999;
    }
  }
}
</style>
